import { getPlansList, getPlanRecord, deleteThePlan } from '@/api/planManage.js'

export default {
    state: {
        //所有计划列表数据
        plansList: [],
        // 计划详情数据
        planDetail: [],
        photosList: [],
        // 当前选中的内容：
        currentPlan: {
            time: "----/--/-- --:--:--",
            title: "请选中一个计划",
            count: "0",
        }
    },
    actions: {
        // 获取无人船计划列表
        async toGetPlansList({ commit }) {
            let res = await getPlansList();
            if (res) {
                commit('setPlansList', res);
            }
            return true
        },
        // 获取单个计划的详细内容
        toGetPlanRecord({ commit }, planId) {
            getPlanRecord(planId).then(res => {
                commit('setPlanDetail', res);
            }).catch(err => {
                console.log(err);
            })
        },
        // 删除计划
        async toDeleteThePlan({ dispatch }, planId) {
            let result = await deleteThePlan(planId);
            if (result === 'success') {
                dispatch('toGetPlansList');
                return 'ok'
            } else {
                return Promise.reject(new Error('failed'))
            }
        }
    },
    mutations: {
        // 存放无人船计划列表数据
        setPlansList(state, listData) {
            state.plansList = listData;
        },
        // 存放单个计划的详细内容
        setPlanDetail(state, detail) {
            state.planDetail = detail;
            // 对图片列表进行清空
            state.photosList = [];
        },
        // 存放当前选中的展示内容
        setCurrentPlan(state, row) {
            let { time, title, count } = row;
            state.currentPlan = { time, title, count };
        },
        // 存放当前选中的图片列表
        setPhotosList(state, data) {
            state.photosList = data;
        },
    },
    getters: {
        // 计划详情的图片集
        imgUrlList(state) {
            let imgUrlList = [];
            state?.photosList?.map(item => {
                imgUrlList.push({ url: [item.url], pos: [item.lng, item.lat] })
            })
            return imgUrlList;
        },
        // 可执行的自动巡航计划列表
        autoCruisePlans(state) {
            let plans = [];
            if (state.plansList.length !== 0) {
                plans = state.plansList?.filter(item => item.id != 1) || [];
            }
            return plans;
        }
    }
}